import axios from 'axios'
// 创建实例
const service = axios.create({
  baseUrl: '',
  timeout: 5000
})
// 请求拦截器
service.interceptors.request.use(function (config) {
  return config
}, function (error) {
  // 错误请求
  return Promise.reject(error)
})
// 响应拦截器
service.interceptors.response.use(function (response) {
  return response
}, function (error) {
  // 错误响应
  return Promise.reject(error)
})
// 封装post请求
export function myPost(url, params) {
  return new Promise((resolve, reject) => {
    service.post(url, params)
      .then(res => {
        resolve(res)
      })
      .catch((err) => {
        reject(err)
      })
  })
}
// 封装get请求
export function myGet(url, params) {
  return new Promise((resolve, reject) => {
    service.get(url, { params: params })
      .then(res => {
        resolve(res)
      })
      .catch((err) => {
        reject(err)
      })
  })
}
export default {
  myPost(url, params) {
    return myPost(url, params)
  },
  myGet(url, params) {
    return myGet(url, params)
  }
}